<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::errorSummary($model); ?>

<?php

$cs = Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->baseUrl . "/js/jwysiwyg/jquery.wysiwyg.js");
$cs->registerCssFile(Yii::app()->baseUrl . '/js/jwysiwyg/jquery.wysiwyg.css');


$this->widget('application.extensions.jwysiwyg.JWysiwyg', array (
	'registerOnly' => true
));

$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'next_objectives_tree'
	),
	'ui' => array (
		'theme_name' => 'checkbox'
	),
	'options' => array (
		'plugins' => array (
			'checkbox' => array (
				'three_state' => 'false'
			)
		)
	),
	'genDiv' => false
));

echo CHtml::activeHiddenField($model, 'project_id') . "\n";
echo CHtml::activeHiddenField($model, 'id') . "\n";
$form_name = 'TopicObjectiveForm[objectives]';
?>

<h2><?php echo Yii::t('project', 'Objectives for next meeting'); ?></h2>
<div class="yiiForm" id="next_objectives">
	<div id="next_objectives_tree">
	<?php
	displayObjectiveSteps($this, $form_name, $model->steps);
	?>
	</div><!-- yiiForm -->
</div><!-- yiiForm -->

<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', '< Previous'), array('name' => 'prev_button')); ?>
<?php echo CHtml::submitButton(Yii::t('views', 'Next >'), array('name' => 'next_button')); ?>
</div>

<?php

echo CHtml::endForm();

function displayObjectiveSteps($p, $model, $steps) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($steps as $step) {
			if ($step->canBeObjective()) {
				echo "<li id='node_" . $step->id . "' class='open node'>\n";
				echo "\t<a href='#'>" . $step->label . "</a>";
				displayObjectiveTopics($p, $model, $step->topics);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

function displayObjectiveTopics($p, $model, $topics) {
	$image = "";

	if (is_array($topics) && count($topics) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($topics as $topic) {
			if ($topic->canBeObjective()) {
				echo "<li id='node_" . $topic->project_topic_id . "' class='node'>\n\t<a href='#'><ins>&nbsp;</ins></a> ".CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
					'class' => 'toogle_layer',
					'id' => 'img_comment_' . $topic->project_topic_id
				)). " <a href='#'>" . $topic->label . "</a>";
				$p->widget('adviser.widgets.ObjectiveLayer', array (
					'topic_id' => $topic->project_topic_id,
					'columns' => 100,
					'rows' => 5,
					'prefix' => '',
					'objective' => $topic->topic->objective,
					'objective_topic' => $topic->topic->objective_topic,
					'objective_needs' => $topic->topic->objective_needs,
					'objective_contacts' => $topic->topic->objective_contacts,
					'form_name' => $model
				));

				displayObjectiveTopics($p, $model, $topic->children);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

$script =<<<JS
$("div[id^='objective_layer_']").hide();
$("#next_objectives_tree").parent().append('<hr class="separation" />');

init_objectives($('#next_objectives_tree'));

JS;
$cs->registerScript('topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_objective.js', CClientScript::POS_HEAD);
?>
